package com.echo.yixiaowu.util

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.navigation.NavHostController
import com.echo.yixiaowu.route.AppRoute
import com.echo.yixiaowu.util.NavLock.setClip

class NavigationHelper(private val navController: NavHostController) {
    fun navigateTo(route: AppRoute) {
        NavLock.tryNavigate {
            setClip(42)
            navController.navigate(route)
        }
    }

    fun popBackStack() {
        NavLock.tryNavigate {
            navController.popBackStack()
        }
    }

    fun navigateToHomeAndClearBackStack() {
        NavLock.tryNavigate {
            navController.navigate(AppRoute.Home) {
                popUpTo(0) {
                    inclusive = true
                }
            }
        }
    }
}

object NavLock {
    private var isLocked by mutableStateOf(false)
    private var boxClip by mutableIntStateOf(42)

    fun tryNavigate(action: () -> Unit) {
        if (!isLocked) {
            isLocked = true
            action()
        }
    }

    fun unlock() {
        isLocked = false
    }

    fun getLock(): Boolean {
        return isLocked
    }

    fun setClip(boxClip: Int) {
        this.boxClip = boxClip
    }

    fun getClip(): Int {
        return boxClip
    }
}